var $;
var table;
var layer;
var form;
var laydate;
layui.use(['jquery','table','layer','form','laydate'],function () {
    $ = layui.jquery;
    table = layui.table;
    layer = layui.layer;
    form = layui.form;
    laydate = layui.laydate;

    /*时间选择器*/
    laydate.render({
        elem:"#createDate",
        type:"date",
        trigger:'click'
    });
    var deptName = '';
    var trueName = '';
    var createDate = '';
    var status = '';
    var DeptList = table.render({
        elem:'#root',
        url:"/newJurisdiction/getJsDeptList?deptName="+deptName+"&trueName="+trueName+"&createDate="+createDate+"&status="+status,
        page:true,
        limit:10,
        limits:[10,15,20],
        toolbar:"#toptool",
        defaultToolbar: ['','',''],
        cols:[
            [
                {type:"checkbox",align:"center"},
                {type:"numbers",title:"序号",align:"center"},
                {field:"deptName",title:"部门名称",align:"center"},
                {field:"deptDescirbe",title:"部门描述",align:"center"},
                {field:"trueName",title:"创建人",align:"center"},
                {field:"createDate",title:"创建时间",align:"center"},
                {field:"status",title:"启用|停用",align:"center",templet:function (data) {
                        if(data.status == 1){
                            var radio = '<a href="javascript:updateDeptStatus('+data.id+','+data.status+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="启用|停用" lay-filter="switchTest" value="' + data.status + '" checked></a>';
                        }else{
                            var radio = '<a href="javascript:updateDeptStatus('+data.id+','+data.status+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="启用|停用" lay-filter="switchTest" value="' + data.status + '"></a>';
                        }
                        return radio;
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        return "<div>"+
                            "<a href='javascript:edit(" + data.id + ");' style='color: #2F4056;margin-right: 10px'>编辑</a>" +
                            "<a href='javascript:deleteDept(" + data.id + ");' style='color: #2F4056;margin-right: 10px'>删除</a>" +
                            "</div>";
                    }}
            ]
        ]
    });
    /*条件查询*/
    form.on('submit(sub)', function (data) {
        var trueName = $('#trueName').val();
        var status = $('#status').val();
        var deptName = $('#deptName').val();
        var createDate = $('#createDate').val();
        DeptList.reload({
            url:"/newJurisdiction/getJsDeptList?deptName="+deptName+"&trueName="+trueName+"&createDate="+createDate+"&status="+status,
        });
        return false;
    });
    /*重置*/
    form.on('submit(subs)', function (data) {
        $('#trueName').val("");
        $('#status').val("");
        $('#deptName').val("");
        $('#createDate').val("");
        DeptList.reload({
            url:"/newJurisdiction/getJsDeptList?deptName",
        });
    });
    /*修改部门状态*/
    updateDeptStatus = function updateDeptStatus(id,statusDept){
        layer.confirm("确认对此部门进行修改？",{
            icon: 3, title: '提示'
        },function () {
            if(statusDept=="1"){
                var status = 2;
            }else{
                var status = 1;
            }
            var updateDate = new Date();
            updateDate = getTime(updateDate);
            var json = {
                id:id,
                status:status,
                updateId:localStorage.getItem("userId"),
                updateDate:updateDate
            }
            $.ajax({
                url:'/newJurisdiction/updateDeptStatus',
                data:json,
                dataType:'json',
                Type:'post',
                success:function(data){
                    if (data.data) {
                        location.reload();
                    }else{
                        layer.msg("修改失败");
                    }
                }
            });
        }
        )
    }
    /*删除部门*/
    deleteDept = function deleteDept(id){
        layer.confirm("是否确定删除",{
                icon: 3, title: '提示'
            },
            function (index) {
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:{"id":id},
                    url:"/newJurisdiction/deleteDept",
                    success:function (data) {
                        if(data.data){
                            layer.close(index);
                            alert("删除成功");
                            location.reload();
                        }
                    }
                });
            }
        );
    }
    /*获取编辑部门信息*/
    edit = function edit(id) {
        $.ajax({
            type:"post",
            javaType:"json",
            data:{"id":id},
            url:"/newJurisdiction/getDeptById",
            success:function (data) {
                editDept(data);
            }
        })
    }
    /*渲染编辑页面*/
    function editDept(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"编辑部门信息",
            content:"/newquan/newquan-dept-edit.html",
            area:["600px","440px"],
            btn:["确定","取消"],
            yes:function(index){
                var updateDate = new Date();
                updateDate = getTime(updateDate);
                var json = {
                    id:obj.find("#id").val(),
                    deptName:obj.find("#deptName").val(),
                    deptDescirbe:obj.find("#deptDescirbe").val(),
                    updateId:localStorage.getItem("userId"),
                    updateDate:updateDate
                };
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:json,
                    url:"/newJurisdiction/updateDept",
                    success:function (data) {
                        if(data){
                            layer.close(index);
                        }
                        location.reload();
                    }
                })
            },btn2:function(index){
                layer.close(index);
            },
            success:function (layero,index) {
                obj = layero.find("iframe").contents();
                obj.find("#id").val(data.data.id);
                obj.find("#deptName").val(data.data.deptName);
                obj.find("#trueName").val(data.data.trueName);
                obj.find("#createDate").val(data.data.createDate);
                obj.find("#deptDescirbe").val(data.data.deptDescirbe);
            }
        });
    }

    /*添加部门*/
    addDept = function addDept(){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            content:"/newquan/newquan-dept-add.html",
            area:["600px","440px"],
            btn:["确定","取消"],
            yes:function (index) {
                var createDate = new Date();
                createDate = getTime(createDate);
                var json = {
                    deptName:obj.find("#deptName").val(),
                    deptDescirbe:obj.find("#deptDescirbe").val(),
                    parentId:0,
                    createId:localStorage.getItem("createId"),
                    status:obj.find("#status").val(),
                    createDate:createDate
                }
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:json,
                    url:"/newJurisdiction/addDept",
                    success:function (data) {
                        if(data.data){
                            alert("添加成功");
                            layer.close(index);
                            location.reload();
                        }else{
                            alert("服务器错误");
                            layer.close(index);
                            location.reload();
                        }
                    }
                });
            },btn2:function (index) {
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find("iframe").contents();
            }
        });
    };
    /*返回*/
    goBack = function goBack() {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    }
    /*刷新*/
    Refresh = function Refresh() {
        location.reload();
    };
    /*获取当前时间*/
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };
    /*时间补零*/
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
});